package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.feign.UserFeign;
import com.usian.article.mapper.*;
import com.usian.article.service.ArticleInfoService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.*;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.behavior.pojos.*;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.utils.threadlocal.UserIdThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleInfoServiceImpl extends BaseServiceImpl<ApArticleMapper, ApArticle> implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper configMapper;

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Autowired
    private ApCollectionMapper apCollectionMapper;

    @Autowired
    private UserFeign userFeign;

    @Value("${file.url}")
    private String fileUrl;

    @Autowired
    private ApArticleContentMapper contentMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        if (StringUtils.isEmpty(dto.getArticleId())) return errorParam;

        HashMap<String, Object> map = new HashMap<>();
        ApArticle apArticle = getById(dto.getArticleId());
        if (apArticle == null) return ResponseResult.errorResult("文章不存在");

        ApArticleConfig apArticleConfig = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if (apArticleConfig == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        if (apArticleConfig.getIsDown() == 0 && apArticleConfig.getIsDelete() == 0) {
            ApArticleContent apArticleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            if (apArticleContent == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            map.put("config", apArticleConfig);
            map.put("content", apArticleContent);
            ResponseResult<Object> result = new ResponseResult<>();
            result.setHost(fileUrl);
            result.setData(map);
            return result;
        }

        return ResponseResult.errorResult("文章已下架或删除");
    }

    @Autowired
    private ApAuthorMapper authorMapper;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //1.检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        Integer userId = UserIdThreadLocalUtils.getUserId();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        if(apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean isUnlike=false,isLike = false,isCollection = false,isFollow = false;

        //3.查询不喜欢行为
        ApUnlikesBehavior apUnlikesBehavior = behaviorFeign.findUnLikeByArticleIdAndEntryId(apBehaviorEntry.getId(), dto.getArticleId());
        if(apUnlikesBehavior != null && apUnlikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isUnlike = true;
        }

        //4.查询点赞行为
        ApLikesBehavior apLikesBehavior = behaviorFeign.findLikeByArticleIdAndEntryId(dto.getArticleId(), apBehaviorEntry.getId(),ApLikesBehavior.Type.ARTICLE.getCode());
        if(apLikesBehavior != null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()){
            isLike = true;
        }

        //5.查询收藏行为
        ApCollection apCollection = apCollectionMapper.selectOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, apBehaviorEntry.getId())
                .eq(ApCollection::getArticleId, dto.getArticleId()).eq(ApCollection::getType, ApCollection.Type.ARTICLE.getCode()));
        if(apCollection != null){
            isCollection = true;
        }

        //6.查询是否关注
        ApAuthor apAuthor = authorMapper.selectById(dto.getAuthorId());
        if(apAuthor != null){
            ApUserFollow apUserFollow = userFeign.findByUserIdAndFollowId(userId, apAuthor.getUserId());
            if(apUserFollow != null){
                isFollow = true;
            }
        }

        //7.结果返回  {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("isfollow",isFollow);
        resultMap.put("islike",isLike);
        resultMap.put("isunlike",isUnlike);
        resultMap.put("iscollection",isCollection);
        return ResponseResult.okResult(resultMap);
    }


}
